package com.khtn.webapp;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.khtn.model.Tour;
import com.khtn.service.TourService;

@Controller
public class TourController {
	
	@Autowired(required=true)
	protected TourService tourService;
	
	@RequestMapping(value="/")
	public String index()
	{
		return "index";
	}
	@RequestMapping(value="detail")
	public String detail()
	{
		return "detail";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list_tour", produces = "application/json",method=RequestMethod.GET)
	public ResponseEntity<String> listTour() {
		HttpHeaders respHeader = new HttpHeaders();
		respHeader.setContentType(new MediaType("application", "json"));
		JSONArray tourJson = new JSONArray();
		Collection<Tour> listTour = new ArrayList<Tour>(); 
		listTour=tourService.findTenNewTour();
		for (Tour tour : listTour) {
			
			tourJson.add(tour.toJson());
		}
		return new ResponseEntity<String>(tourJson.toJSONString(), respHeader,
				HttpStatus.OK);
	}
}
